# application/services/system/dict.py
from application.mapper.system.dict_type import DictTypeMapper
from typing import Dict, Any, List
from application.models.system.dict_data import SysDictData
from application.models.system.dict_type import SysDictType
from application.utils.res import models_to_dicts_camel, to_snake_case


class DictTypeService:
    """字典服务层"""
    @staticmethod
    def get_dict_type_optionselect() -> List[Dict[str, Any]]:
        """
        获取字典类型下拉列表
        :return: 字典类型列表
        """
        dict_types = DictTypeMapper.get_all_dict_types()

        result_list = models_to_dicts_camel(dict_types)

        return result_list


    @staticmethod
    def create_dict_type(data: Dict[str, Any]) -> Dict[str, Any]:
        """
        创建产品型号
        :param product_model_data: 产品型号数据
        :return: 创建后的产品型号数据
        """
        # 创建产品型号对象
        dict_type = SysDictType()

        for key, value in data.items():

            snake_key = to_snake_case(key)
            if hasattr(dict_type, snake_key):
                setattr(dict_type, snake_key, value)
        
        created_model = DictTypeMapper.create_dict_type(dict_type)
        return models_to_dicts_camel([created_model])[0]
    

    
    @staticmethod
    def update_dict_type(dict_Id: int, dict_Type_data: Dict[str, Any]) -> Dict[str, Any]:
        """
        更新字典数据
        :param dict_code: 字典数据ID
        :param product_model_data: 字典数据数据
        :return: 更新后的字典数据数据
        """
        dict_type_model = DictTypeMapper.get_dict_Type_by_id(dict_Id)
        
        if not dict_type_model:
            return None
            
        # 更新字典数据对象
        for key, value in dict_Type_data.items():
            snake_key = to_snake_case(key)
            if hasattr(dict_type_model, snake_key):
                setattr(dict_type_model, snake_key, value)
        
        updated_model = DictTypeMapper.update_dict_data(dict_type_model)
        return models_to_dicts_camel([updated_model])[0]
